package palms.client.android.apis.services;

import com.example.android.google.apis.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import palms.client.android.apis.view.PalmsMapView;
import palms.client.domain.User;
import palms.client.manager.ClientManager;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class LocationService extends Service implements LocationListener{

	private LocationManager locationManager;
	private static PalmsMapView palmsMapView;
	private MapController mapController;
	private ClientManager clientManager;
	private User user;
	
	public static void setActivity(PalmsMapView palmsMapView){
		setPalmsMapView(palmsMapView);
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate(){
		
		this.user = getPalmsMapView().getNSUser();
		
		this.locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
		this.getLocationManager().requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);

		
		if(this.user.getLatitude() == 0 && this.user.getLongitude() == 0){
			getPalmsMapView().getNSUser().setLatitude(38.736830);
			getPalmsMapView().getNSUser().setLongitude(-9.138181);
		}
		
		GeoPoint geo = new GeoPoint((int)(this.user.getLatitude()*1E6), (int)(this.user.getLongitude()*1E6));
		
				
		this.clientManager = getPalmsMapView().getClientManager();
		
		if(!clientManager.getUser().getService().equals("")){
			String result = null;
			while(result == null)
				result = clientManager.getService();
			Log.d("PALMs", result);
		}
				
		MapView pmv = (MapView)getPalmsMapView().findViewById(R.id.mapview);   	
        this.mapController = pmv.getController();
        this.mapController.setZoom(16);
        this.mapController.setCenter(geo);
        
	}

	@Override
	public void onDestroy(){
		this.getLocationManager().removeUpdates(this);
	}
	public void onLocationChanged(Location location) {
		getPalmsMapView().getNSUser().setLatitude(location.getLatitude());
		getPalmsMapView().getNSUser().setLongitude(location.getLongitude());
        GeoPoint geo = new GeoPoint(
        	    (int)(getPalmsMapView().getNSUser().getLatitude()*1E6),
        	    (int)(getPalmsMapView().getNSUser().getLongitude()*1E6));
        
		Log.d("CENAS", "My geolocation is " + geo.getLatitudeE6() + ", " + geo.getLongitudeE6());

		this.mapController.animateTo(geo);
		this.mapController.setCenter(geo);
	
		if(!clientManager.getUser().getService().equals("")){
			String result = null;
			while(result == null)
				result = clientManager.getService();
			Log.d("CENAS", result);
		}
	}

	public void onProviderDisabled(String provider) {
		Intent intent = new Intent(
				android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		startActivity(intent);
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	public static PalmsMapView getPalmsMapView() {
		return palmsMapView;
	}

	public static void setPalmsMapView(PalmsMapView palmsMapView) {
		LocationService.palmsMapView = palmsMapView;
	}

	public LocationManager getLocationManager() {
		return locationManager;
	}

	public void setLocationManager(LocationManager locationManager) {
		this.locationManager = locationManager;
	}

	public ClientManager getClientManager() {
		return clientManager;
	}

	public void setClientManager(ClientManager clientManager) {
		this.clientManager = clientManager;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
}
